/********************************************/
/*	Copyright (C) 2018 Gong Li Bin			*/
/*	Project:	GmRm2000-1.0.0				*/
/*	Author:		gong_libin					*/
/*	Date:		2018_07_01					*/
/*	File:		GmHttp.go					*/
/********************************************/

package proto

import (
	"fmt"
	"strings"
)

type GmHttp struct {
	HttpMap map[string] string
}

func NewGmHttp(buf []byte) *GmHttp {
	s := string(buf)
	http := new(GmHttp)
	http.HttpMap = make(map[string] string)

	for _, ln := range strings.Split(s, "\r\n") {
		if len(ln) > 0 {
			idx := strings.IndexByte(ln, ':')
			if -1 != idx {
				http.HttpMap[strings.TrimSpace(string(ln[0:idx]))] = strings.TrimSpace(string(ln[idx+1:]))
			} else {
				http.HttpMap[ln] = ""
			}
		}
	}

	return http
}

func (v GmHttp)GmPrint() {
	for k, v := range v.HttpMap {
		if len(v) > 0 {
			fmt.Printf("%s: %s\n", k, v)
		} else {
			fmt.Printf("%s\n", k)
		}
	}
}